package com.poetize.plus.controller.ui;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.poetize.plus.entity.dto.page.ArticlePageDTO;
import com.poetize.plus.entity.vo.ArticleVO;
import com.poetize.plus.entity.vo.page.ArticlePageVO;
import com.poetize.plus.service.ArticleDislikeService;
import com.poetize.plus.service.ArticleLikeService;
import com.poetize.plus.service.ArticleService;
import com.poetize.plus.utils.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.security.PermitAll;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zuosy
 * @created 2024/4/10 14:25
 */
@Tag(name = "文章操作")
@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
public class ArticleUiController {

    private final ArticleService articleService;

    private final ArticleLikeService articleLikeService;

    private final ArticleDislikeService articleDislikeService;

    @PermitAll
    @GetMapping("/getArticleById/{id}")
    @Operation(summary = "根据ID获取文章详情")
    public R<ArticleVO> getArticleById(@PathVariable("id") Long id){
        return R.ok(articleService.getArticleById(id));
    }

    @PermitAll
    @GetMapping("/getArticlePage")
    @Operation(summary = "获取分页文章")
    public R<Page<ArticlePageVO>> getArticleBySortOrLabel(ArticlePageDTO articlePageDTO){
        articlePageDTO.setViewStatus(true);
        return R.ok(articleService.articlePage(articlePageDTO));
    }

    @GetMapping("/like/{articleId}")
    @Operation(summary = "文章点赞")
    @PreAuthorize(value = "hasRole('common')")
    public R<Boolean> likeArticle(@PathVariable("articleId") Long articleId){
        return R.ok(articleLikeService.likeArticle(articleId));
    }

    @GetMapping("/dislike/{articleId}")
    @Operation(summary = "文章点踩")
    @PreAuthorize(value = "hasRole('common')")
    public R<Boolean> dislikeArticle(@PathVariable("articleId") Long articleId){
        return R.ok(articleDislikeService.dislikeArticle(articleId));
    }

    @PermitAll
    @GetMapping("/checkPassword/{id}/{password}")
    @Operation(summary = "校验密码")
    public R<Boolean> checkPassword(@PathVariable("id") Long id,
                                    @PathVariable("password") String password){
        return R.ok(articleService.checkPassword(id, password));
    }
}
